package com.rocsea.robot.service;

import com.rocsea.robot.config.build.KettleBuildConfig;
import com.rocsea.robot.config.jdbc.common.JdbcConfig;
import com.rocsea.robot.factory.KettleBuildDatabaseFactory;
import com.rocsea.robot.model.bo.KettleTaskConfig;
import com.rocsea.robot.model.enums.KettleBuildModuleEnum;
import com.rocsea.robot.utils.DatabaseUtils;
import com.rocsea.robot.utils.KettleBuildUtils;
import com.rocsea.robot.utils.MysqlBuilderUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static com.rocsea.robot.constant.KettleBuildConstant.DATE_FORMAT_SEC;
import static com.rocsea.robot.constant.KettleTaskConfigBuildConstant.*;

/**
 * @Author RocSea
 * @Date 2023/1/31
 */
@Component
public class KettleTaskConfigService {
    @Resource
    public KettleBuildConfig buildConfig;

    private static final int MAP_INITIAL_CAPACITY = 2;

    public String buildKettleTaskConfig(Boolean compensationFlag) throws ParseException {
        String taskName = KettleBuildUtils.getSourceTableName();
        String sourceSchemaName = KettleBuildUtils.getSourceSchemaName();
        if (StringUtils.isNotEmpty(buildConfig.getOutDatabase())) {
            taskName = sourceSchemaName + "." + taskName;
        }
        Boolean r = ifBuildKettleTaskConfig(taskName);
        StringBuilder kettleTaskConfig = new StringBuilder();
        if (r) {
            // build normal task config
            KettleTaskConfig normal = createKettleTaskConfig();
            normal.setNextEndTime(DateUtils.parseDate(END_TIME, DATE_FORMAT_SEC));
            normal.setSyncStatus(0);
            normal.setTaskName(taskName);
            normal.setCompensationType(2);
            kettleTaskConfig.append(MysqlBuilderUtils.buildKettleTaskConfig(normal));

            // if need build compensation task config
            if (compensationFlag) {
                KettleTaskConfig compensation = createKettleTaskConfig();
                compensation.setNextEndTime(DateUtils.parseDate(START_TIME, DATE_FORMAT_SEC));
                compensation.setSyncStatus(1);
                compensation.setTaskName(taskName + "(task_compensation)");
                compensation.setCompensationType(1);
                kettleTaskConfig.append(MysqlBuilderUtils.buildKettleTaskConfig(compensation));
            }
        }
        return kettleTaskConfig.toString();
    }

    private Boolean ifBuildKettleTaskConfig(String taskName) {
        JdbcConfig jdbcConfig = KettleBuildDatabaseFactory.getDatabase(KettleBuildModuleEnum.ONSHORE.getText());
        Map<String, Object> params = new HashMap<>(MAP_INITIAL_CAPACITY);
        params.put(COLUMN_TASK_NAME, taskName);
        Optional<Long> tableCount = DatabaseUtils.getTableCount(jdbcConfig.getUrl(), jdbcConfig.getUsername(),
                jdbcConfig.getPassword(), SYNC_CONFIG_SCHEMA_NAME, SYNC_CONFIG_TABLE_NAME, params);
        return tableCount.orElse(0L) < 1;
    }

    private KettleTaskConfig createKettleTaskConfig() throws ParseException {
        KettleTaskConfig kettleTaskConfig = new KettleTaskConfig();
        kettleTaskConfig.setNextInsertId(0L);
        kettleTaskConfig.setNextUpdateId(0L);
        kettleTaskConfig.setNextStartTime(DateUtils.parseDate(START_TIME, DATE_FORMAT_SEC));
        kettleTaskConfig.setTaskType(1);
        return kettleTaskConfig;
    }
}
